<template>
  <div class="login-container">
    <div class="login-form-container">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left">
        <div style="text-align: center;margin: 30px 0 15px 0;font-size: 20px;color:#18AEEA;font-weight: bold">用 户 登 录
        </div>
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            name="username"
            type="text"
            auto-complete="on"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"/>
        </el-form-item>
        <el-form-item prop="password" class="el-form-item-password">
          <el-input
            :type="pwdType"
            v-model="loginForm.password"
            name="password"
            auto-complete="on"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            @keyup.enter.native="handleLogin"/>
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="pwdType === 'password' ? 'eye' : 'eye-open'"/>
          </span>
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" type="primary" style="width:75%; font-size: 20px" @click.native.prevent="handleLogin">
            登 陆
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="login-title-container">
      <img src="../../assets/img/login/title.png" alt="">
    </div>
  </div>
</template>

<script>
  import { isvalidUsername } from '@/utils/validate'

  export default {
    name: 'Login',
    data: function() {
      const validateUsername = (rule, value, callback) => {
        if (!isvalidUsername(value)) {
          callback(new Error('请输入正确的用户名'))
        } else {
          callback()
        }
      }
      const validatePass = (rule, value, callback) => {
        if (value.length < 5) {
          callback(new Error('密码不能小于5位'))
        } else {
          callback()
        }
      }
      return {
        loginForm: {
          username: 'admin',
          password: '123456'
        },
        loginRules: {
          username: [{ required: true, trigger: 'blur', validator: validateUsername }],
          password: [{ required: true, trigger: 'blur', validator: validatePass }]
        },
        loading: false,
        pwdType: 'password',
        redirect: undefined
      }
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect
        },
        immediate: true
      }
    },
    methods: {
      showPwd() {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store.dispatch('user/Login', this.loginForm).then((res) => {
              this.loading = false
              this.$router.push({ path: this.redirect || '/navigation' })
            }).catch(() => {
              this.loading = false
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss">
  .login-container {
    background-image: url(../../assets/img/login/bg.png);
    background-size: 100% 100%;
    background-repeat: no-repeat;
    height: 100%;
  }

  .login-form-container {
    background-image: url(../../assets/img/login/loginform.png);
    background-size: 100% 100%;
    background-repeat: no-repeat;
    width: 425px;
    height: 295px;
    position: absolute;
    right: 10%;
    top: 38%;
  }

  .login-form .el-input {
    display: inline-block;
    height: 47px;
    width: 75%;
    input {
      background: transparent;
      border: 2px solid #1C81BE;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 35px;
      color: white;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px #2d3a4b inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
</style>

<style rel="stylesheet/scss" lang="scss" scoped>

  .login-title-container {
    text-align: center;
    padding-top: 4%;
  }

  .show-pwd {
    position: absolute;
    color:#18AEEA ;
    padding: 0 5px;
  }

  .login-form {
    text-align:center;
  }

  .el-form-item-password{
    position: relative;
  }

</style>
